﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ShopOnline
{
    /// <summary>
    /// TODO:Put a description here.
    /// </summary>
    [Serializable]
    public class ProductImgRec
    {
        #region Private Members
        private Int64 m_id = 0;
        private Int64 m_productId = 0;
        private String m_imgUrl = String.Empty;
        private DateTime m_createDate = DateTime.MinValue;
        private int m_status = -1;
        #endregion

        #region Public Properties
        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Int64 id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Int64 productId
        {
            get { return m_productId; }
            set { m_productId = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String imgUrl
        {
            get { return m_imgUrl; }
            set { m_imgUrl = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public DateTime createDate
        {
            get { return m_createDate; }
            set { m_createDate = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public int status
        {
            get { return m_status; }
            set { m_status = value; }
        }

        #endregion

        #region  Properties Const

        private const string ConnectName = "ShopOnline";
        private const string Folderlogs = "ProductImg";

        #endregion

        #region Store Procedure
        private String usp_GetList_ProductImg = "usp_GetList_ProductImg";
        private String usp_SetList_ProductImg = "usp_SetList_ProductImg";
        #endregion


        #region Init
        public ProductImgRec() { }
        public ProductImgRec(Int64 id, Int64 productId, String imgUrl, DateTime createDate, int status)
        {
            m_id = id;
            m_productId = productId;
            m_imgUrl = imgUrl;
            m_createDate = createDate;
            m_status = status;
        }
        #endregion

        #region Call Store Procedure
        public List<ProductImgRec> GetList(Int64 ProductID) {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetList - ProductID:" + ProductID;
            try
            {
                List<ProductImgRec> records = new List<ProductImgRec>();
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetList_ProductImg, ProductID);

                #region Default variables
                Int64 id = 0;
                Int64 productid = 0;
                String imgurl = String.Empty;
                DateTime createdate = DateTime.MinValue;
                int status = -1;
                #endregion

                if (dsResult.Tables[0].Rows.Count > 0)
                {

                    foreach (DataRow row in dsResult.Tables[0].Rows)
                    {
                        if (row.IsNull("id") == false)
                            id = (Int64)row["id"];
                        if (row.IsNull("productId") == false)
                            productid = (Int64)row["productId"];
                        if (row.IsNull("imgUrl") == false)
                            imgurl = row["imgUrl"].ToString();
                        if (row.IsNull("createDate") == false)
                            createdate = (DateTime)row["createDate"];
                        if (row.IsNull("status") == false)
                            status = (Byte)row["status"];
                        records.Add(new ProductImgRec(id, productid, imgurl, createdate, status));
                    }

                }

                if (records.Count <= 0)
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                }

                return records;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new List<ProductImgRec>();
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }

        }

        public ObjectResult SetList(Int64 ProductID, List<ProductImgRec> listProductImg) {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetList - Count listProductImg:" + listProductImg.Count();
            try
            {
                int NuItem = listProductImg.Count();
                string XmlData = "";
                XmlData += "<root>";
                int Rank = 1;
                foreach (ProductImgRec item in listProductImg) {
                    XmlData += "<item rank='" + Rank + "' id='" + item.id + "' imgUrl='" + item.imgUrl + "'></item>";
                    Rank++;
                }
                XmlData += "</root>";


                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_SetList_ProductImg, ProductID, NuItem, XmlData);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }
        #endregion
    }

}
